// 新建文件夹
import React, { useState } from 'react';
import { Form, Input, message, Modal } from 'antd';
import { createAnalyseCategory, updateAnalyseCategory } from 'src/services/AnalyseManage';

interface Iprops {
	modalVisible: boolean;
	isUpdate?: any;
	updateFolderData?: any;
	folderData?: any;
	handleCancel: Function;
	onSuccess: Function;
}

// eslint-disable-next-line react/display-name
const CreateFolder = React.forwardRef( ( props: Iprops, formRef: any ) => {
	const [ form ] = Form.useForm();
	const defaultValue = props.isUpdate ? props.updateFolderData : { folderName: '', des: '' };
	const [ formData ] = useState( defaultValue );


	const handleCancel = () => {
		form.resetFields();
		props.handleCancel();
	};

	const handleOk = () => {
		form.validateFields()
			.then( ( value ) => {
				handleCreate( value );
				// handleCancel();
			} )
			.catch( ( info ) => {
				message.warning( '请完善信息' );
			} );
	};

	//新增文件夹/更新文件夹
	const handleCreate = async ( values: any ) => {
		
		let response;
		if( props.isUpdate ) {
			let params = { name: values.name, des: values.des, id: props.updateFolderData.id };
			response = await updateAnalyseCategory( params );
		} else {
			let params = { name: values.name, des: values.des, parentId: '788094201617166336', type: 'COMPONENT' };
			response = await createAnalyseCategory( params );
		}

		if ( response.data && response.data.success ) {
			props.onSuccess();
			props.handleCancel();
		}
	};
	return (
		<Modal
			okText={'确定'}
			visible={props.modalVisible}
			cancelText={'取消'}
			onCancel={handleCancel}
			onOk={handleOk}
			title={props.isUpdate ? '更新文件夹' : '添加文件夹'}			
		>
			<Form form={form} ref={formRef} labelCol={{ span: 4 }} initialValues={formData}>
				<Form.Item label={'名称'} rules={[ { required: true, message: '名称不能为空' } ]} name="name">
					<Input autoComplete="off" />
				</Form.Item>
				<Form.Item label={'描述'} name="des">
					<Input />
				</Form.Item>
			</Form>
		</Modal>
	);
} );

export default CreateFolder;
